package com.ssyt.hiring.web.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.ssyt.cdp.common.BaseDao;
import com.ssyt.cdp.common.PageModel;
import com.ssyt.cdp.common.util.StringUtil;
import com.ssyt.hiring.entity.PositionEntity;

@Repository
public class PositionDao extends BaseDao<PositionEntity, Integer> {

	public PageModel<PositionEntity> listAll(String queryStr, int pageNo,
			int pageSize) {
		List<Object> paramList = new ArrayList<>();
		StringBuffer hql = new StringBuffer();
		hql.append("SELECT new PositionEntity(p.name, p.salary1, p.salary2, p.releaseTime, c.name AS companyName, c.slogan AS companySlogan, c.address AS companyAddress) ");
		hql.append("FROM PositionEntity p, CompanyEntity c ");
		hql.append("WHERE p.companyId=c.id ");
		if (StringUtil.isNotBlank(queryStr)) {
			hql.append("AND p.name LIKE ? OR c.name LIKE ? ");
			paramList.add("%" + queryStr + "%");
			paramList.add("%" + queryStr + "%");
		}
		hql.append("ORDER BY p.id DESC ");
		return pageEntityByHql(pageNo, pageSize, hql.toString(), paramList);
	}

	/**
	 * 发布职位
	 * 
	 * @param position
	 */
	public void addposition(PositionEntity position) {
		position.setReleaseTime(new Date());
		save(position);
	}

	/**
	 * 查询公司发布的所有职位
	 * 
	 * @param companyId
	 * @return
	 */
	public List<PositionEntity> positionList(int companyId) {
		String hql = "FROM PositionEntity WHERE companyId=?";
		return listEntityByHql(hql, companyId);
	}

	/**
	 * 删除职位
	 * 
	 * @param position
	 */
	public void deleteposition(int positionId) {
		PositionEntity position = load(positionId);
		delete(position);
	}

	/**
	 * 修改前查询职位信息
	 * 
	 * @param positionId
	 * @return
	 */
	public PositionEntity getInfoPosition(int positionId) {
		String hql1 = "FROM PositionEntity WHERE id=?";
		return uniqueEntityByHql(hql1, positionId);

	}

	/**
	 * 修改职位信息
	 * 
	 * @param position
	 */
	public void updatePosition(PositionEntity position) {
		update(position);
	}

	/**
	 * 检查发布的职位名是否重复
	 */
	public boolean checkPositionName(String name) {
		String hql = "From PositionEntity where name=?";
		PositionEntity position = uniqueEntityByHql(hql, name);
		if (position != null) {
			return false;
		}
		return true;
	}

}
